package com.zeli.action.filter;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class LanguageFilter implements Filter {
	
	protected FilterConfig filterConfig = null;

	@Override
	public void destroy() {
		// do nothing...
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpSession session = request.getSession();
		Locale currentLocale = (Locale) session.getAttribute("WW_TRANS_I18N_LOCALE");
		if (currentLocale == null) {
			currentLocale = new Locale("zh", "CN");
		}
		String language = request.getParameter("language");
		if ("zh".equals(language)) {
			currentLocale = new Locale("zh", "CN");
		}else if("en".equals(language)) {
			currentLocale = new Locale("en", "US");			
		} else if ("jp".equals(language)) {
			currentLocale = new Locale("ja", "JP");
		}
		session.setAttribute("WW_TRANS_I18N_LOCALE", currentLocale);
		session.setAttribute("language", currentLocale.getLanguage());
		chain.doFilter(servletRequest, servletResponse);
		
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
	}
	
}
